import 'package:flutter/material.dart';

class TabDemo1 extends StatefulWidget {
  List<Widget> _tabsView = [Tab1(), Tab2(), Tab3()];

  @override
  _TabDemo1State createState() => _TabDemo1State();
}

class _TabDemo1State extends State<TabDemo1>
    with SingleTickerProviderStateMixin {
  List _tabsName = ['阅读', '图片', '视频'];

  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: _tabsName.length, vsync: this);
    _tabController.addListener(() {
      setState(() {
        print('tab切换');
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tabs练习'),
        bottom: TabBar(
          controller: _tabController,
          tabs: _tabsName.map((e) => Tab(text: e)).toList(),
        ),
      ),
      body: TabBarView(
        children: widget._tabsView,
        controller: _tabController,
      ),
    );
  }
}

class Tab1 extends StatelessWidget {
  const Tab1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('111111'),
    );
  }
}

class Tab2 extends StatelessWidget {
  const Tab2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('222222'),
    );
  }
}

class Tab3 extends StatelessWidget {
  const Tab3({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('333333'),
    );
  }
}
